package org.example.myerp.service.serviceimpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.example.myerp.mapper.dto.MaterialCompositionDesignReview;
import org.example.myerp.mapper.dto.MaterialInformationDto;
import org.example.myerp.mapper.productDesign.productMaterialCompositionDesign.ProWuliaoTabMapper;
import org.example.myerp.pojo.PageMyself;
import org.example.myerp.pojo.*;
import org.example.myerp.service.ProWuliaoDetaileService;
import org.example.myerp.service.ProWuliaoTabService;

import org.example.myerp.service.ProductFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProWuliaoTabServiceImpl<ProWuliaoTabMapp> implements ProWuliaoTabService {
	@Autowired
	//�������
    ProWuliaoTabMapper proWuliaoTabMapper;
	@Autowired
	//���������ϸ
    ProWuliaoDetaileService proWuliaoDetaileService;
	@Autowired
	//��Ʒ��������Ƶ���
    org.example.myerp.service.ProductFilesService ProductFilesService;

	@Override
	public int getInsertMaterialCompositionMessage(MaterialInformationDto materialInformationDto) {
		//�ܳɱ�
		double SunPrice = 0;
		//ѭ����ȡ�۸��ܼ�
		for (ProInformation proInformation : materialInformationDto.getList()) {
			SunPrice += proInformation.getPrice() * proInformation.getNum();
		}
		/*
		 * û�и����׵�������һ��Id
		 * materialInformationDto.getpId()����Ʒ���Id
		 * proSigeId:��Ʒ������Ƶ����
		 * ��ȡ���
		 */
		Integer proSigeId = ProductFilesService.getSelectProSigeIdByPid(materialInformationDto.getpId());
		//���������ɻ�����Ϣ
		ProWuliaoTab proWuliaoTab = new ProWuliaoTab(null,proSigeId,"δ���",materialInformationDto.getpId(),SunPrice);
		int row = proWuliaoTabMapper.insertMaterialCompositionMessage(proWuliaoTab);
		//ѭ�����������ϸ������Ϣ����������
		for (ProInformation proInformation : materialInformationDto.getList()) {
			ProWuliaoDetaile MaterialCompositionDetails = new ProWuliaoDetaile(null,proSigeId,proInformation.getpId(),proInformation.getPrice(),proInformation.getNum(),proInformation.getSubtotal(),proInformation.getNum());
			//������������ϸ
			row += proWuliaoDetaileService.getInsertMaterialCompositionDetails(MaterialCompositionDetails);
		}
		/*
		 * �ж��ƶ����������Ƶ��Ƿ�ɹ�
		 * �ɹ������޸Ĳ�Ʒ��������Ƶ�����WuLiaoSigin
		 */
		//�޸Ĳ�Ʒ��������Ƶ���
		row += proWuliaoTabMapper.updateProWuliaoTabRealReviewSign(materialInformationDto.getpId());
		row += ProductFilesService.getUpdateProductFileWuLiaoSiginByProSigeId(proSigeId);
		return row;
	}

	/**
	 * ��ѯ���������Ƶ�δ��˵�ȫ����Ϣ����ҳ��ѯ��
	 */
	@Override
	public PageMyself<MaterialCompositionDesignReview> getSelectAllMaterialCompositionDesignReviewByReviewSign(Integer pageNum, Integer pageSize) {
		// �洢�������в�������
		Map params = new HashMap<String, Object>();
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);
		// ������ҳ�����࣬��Ϊ����ֵ
		PageMyself<MaterialCompositionDesignReview> page = new PageMyself<MaterialCompositionDesignReview>();
		if (pageNum != null && pageSize != null) {
			// ���õ�ǰҳ���ҳ��С
			page.setPageNum(pageNum);
			page.setPageSize(pageSize);
		}
		// ��ȡ��ǰҳ����
		List<MaterialCompositionDesignReview> list = proWuliaoTabMapper.selectMaterialCompositionDesignReviews(params);
		// ���÷�ҳ���������ʾ����
		page.setRows(list);
		// ����������
		page.setTotal(proWuliaoTabMapper.selectMaterialCompositionDesignCount());
		return page;
	}

	/**
	 * �޸Ĳ�Ʒ������ɵ�����ֶΡ�����ˡ�
	 */
	@Override
	public Integer getUpdateProWuliaoTabRealReviewSign(MaterialInformationDto materialInformationDto) {
		int row = 0;
		//ѭ���޸Ĳ�Ʒ���������ϸ
		for (ProInformation proInformation : materialInformationDto.getList()) {
			row += proWuliaoDetaileService.getUpdateProWuliaoDetaileRealNumAndCountByProSigeId(materialInformationDto.getId(), proInformation.getNum(),proInformation.getSubtotal());
		}
		//�޸Ĳ�Ʒ������ɵ�ReviewSign
		row += proWuliaoTabMapper.updateProWuliaoTabRealReviewSign(materialInformationDto.getId());
		System.out.println(1);
		row += ProductFilesService.getUpdateProductFilesByPId(materialInformationDto.getpId());
		System.out.println(2);
		return row;
	}

}
